-- UIShipSelectTarget
-- Created by chengb Jun/5/2015
-- 幽灵船选择目标

UIShipSelectTarget = class("UIShipSelectTarget", function()
    return cc.Layer:create();
end);

function UIShipSelectTarget.create(skillId)
    return UIShipSelectTarget.new(skillId);
end

function UIShipSelectTarget:ctor(skillId)
    -- 初始化
    self:setName("UIShipSelectTarget");

    local node = cc.CSLoader:createNode("layout/bine/BineSelectTarget.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.skillId = skillId;
    self.ship    = ME.user.ship;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIShipSelectTarget:resize()
    local node = self.node;
    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;

    local scaleX = w / DESIGN_WIDTH;
    local scaleY = h / DESIGN_HEIGHT;

    -- CT底部居中并缩放至合适大小
    AlignM.alignToBottomCenter(node, "CT");

    -- CT2顶部居中并缩放至合适大小
    AlignM.alignToTopCenter(node, "CT2");

    -- 蒙版需要拉伸至正常大小
    local CT = findChildByName(node, "CT");
    local CT2 = findChildByName(node, "CT2");
    local bg1 = findChildByName(node, "CT/bg1");
    local bg2 = findChildByName(node, "CT2/bg2");
    bg1:setScale(scaleX / CT:getScale(), scaleY / CT:getScale());
    bg2:setScale(scaleX / CT2:getScale(), scaleY / CT:getScale());

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UIShipSelectTarget:redraw()
    -- 显示遮罩
    self:showMask();

    -- 显示幽灵船模型
    self:redrawModel();

    -- 技能效果
    local effect = findChildByName(self.node, "CT2/desc");
    local effectText = SkillM.getSkillDesc(self.skillId);
    effectText = string.gsub(effectText, "%[blue]", "[light_green]");
    TextStyleM.setTextStyle(effect, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BROWN_YELLOW);
    applyString(effect, effectText);

    -- 提示信息
    local tipLabel = findChildByName(self.node, "CT/tip");
    TextStyleM.setTextStyle(tipLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BROWN_YELLOW);
    tipLabel:setString(getLocStr("no_select_target"));

    -- 隐藏施法消耗
    local mpNode = findChildByName(self.node, "CT/lanping");
    mpNode:setVisible(false);
end

-- 显示幽灵船模型
function UIShipSelectTarget:redrawModel()
    -- 创建模型
    local shipDisplay = ShipDisplay.create(self.ship.classId);

    -- 设置缩放比例
    shipDisplay:setScale(1.0);

    -- 设置偏移量
    local offsetX = 110;
    local offsetY = 720;

    shipDisplay:setPosition(tonumber(offsetX), tonumber(offsetY));

    local modelNode = findChildByName(self.node, "CT/model_node");
    modelNode:removeAllChildren();
    modelNode:addChild(shipDisplay);
end

-- 显示蒙版
function UIShipSelectTarget:showMask()
   showTargetSelectMask(self.targetType);
end

-- 隐藏蒙版
function UIShipSelectTarget:hideMask()
    hideTargetSelectMask();
end

-- 注册点击事件
function UIShipSelectTarget:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIShipSelectTarget");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end

-- 判断施放有攻击目标
function UIShipSelectTarget:hasTarget()
    local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
    for _, monsterGrid in pairs(openMonsterGrids) do
        local monster = monsterGrid.gridData.monster;
        if monster and not monster:isDead() then
            return true;
        end
    end

    return false;
end

-- 注册事件处理回调函数
function UIShipSelectTarget:registerEventCallback()
    EventMgr.register("UIShipSelectTarget", event.TARGET_SELECTED, function(pos)
        -- 判断是否有攻击目标
        if not self:hasTarget() then
            alert(getLocStr("no_attack_target"));

            self:hideMask();
            UIMgr.getCurrentScene():removeFormByName("UIShipSelectTarget")
            return;
        end

        -- 判断能否使用技能
        local ret = GhostShipM.canUseSkill(self.skillId);
        if ret ~= true then
            -- 提示无法使用
            alert(getLocStr("skill_cd_tip"));
        else
            -- 抛出幽灵船施法前摇事件
            -- 待施法前摇结束后开始使用技能
            EventMgr.fire(event.SHIP_PRE_CAST, { ["skillId"] = self.skillId, });
        end

        self:hideMask();

        -- 关闭当前界面
        UIMgr.getCurrentScene():removeFormByName("UIShipSelectTarget");
    end);

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            self:hideMask();

            EventMgr.removeAll("UIShipSelectTarget");

            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif ev == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end





